﻿using System;

#if WINDOWS_PHONE
namespace Fibre.WP7.Threading
#else
#if SILVERLIGHT
namespace Fibre.Silverlight.Threading
#else
namespace Fibre.Threading
#endif
#endif
{
    public static class Execute
    {
        public static void AsyncPattern(
            Func<AsyncCallback, object, IAsyncResult> work,
            Action<IAsyncResult> complete,
            int timeout = 5000)
        {
            AsyncSynchroniser.MakeSynchronous(work, complete, timeout)();
        }

        public static T AsyncPattern<T>(
            Func<AsyncCallback, object, IAsyncResult> work,
            Func<IAsyncResult, T> complete,
            int timeout = 5000)
        {
            return AsyncSynchroniser.MakeSynchronous(work, complete, timeout)();
        }

        public static void AsyncPattern<T>(
            Func<T, AsyncCallback, object, IAsyncResult> work, 
            T asyncArgument,
            Action<IAsyncResult> complete,
            int timeout = 5000)
        {
            AsyncSynchroniser.MakeSynchronous(work, asyncArgument, complete, timeout)();
        }

        public static TResult AsyncPattern<TArg, TResult>(
            Func<TArg, AsyncCallback, object, IAsyncResult> work, 
            TArg asyncArgument,
            Func<IAsyncResult, TResult> complete,
            int timeout = 5000)
        {
            return AsyncSynchroniser.MakeSynchronous(work, asyncArgument, complete, timeout)();
        }

        public static void AsyncPattern<TArg, TArg2>(
            Func<TArg, TArg2, AsyncCallback, object, IAsyncResult> work,
            TArg asyncArgument, 
            TArg2 asyncArgument2,
            Action<IAsyncResult> complete,
            int timeout = 5000)
        {
            AsyncSynchroniser.MakeSynchronous(work, asyncArgument, asyncArgument2, complete, timeout)();
        }

        public static TResult AsyncPattern<TArg, TArg2, TResult>(
            Func<TArg, TArg2, AsyncCallback, object, IAsyncResult> work,
            TArg asyncArgument,
            TArg2 asyncArgument2,
            Func<IAsyncResult, TResult> complete,
            int timeout = 5000)
        {
            return AsyncSynchroniser.MakeSynchronous(work, asyncArgument, asyncArgument2, complete, timeout)();
        }
    }
}
